package com.netty.server.threadpool;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 作者 : 陈怀傲
 * @version 创建时间：2015年4月18日 下午3:21:34 类说明
 */
public class ExecutorPool {
	private static ExecutorPool singleton;
	private static ThreadPoolExecutor poolExecutor;
	private static int maxThread = 100;

	private ExecutorPool() {
		poolExecutor = newFixedThreadPool(maxThread);
	}

	public static ExecutorPool getSingleton() {
		if (singleton == null) {
			synchronized (ExecutorPool.class) {
				if (singleton == null) {
					singleton = new ExecutorPool();
				}
			}
		}
		return singleton;
	}

	/**
	 * 得到线程池
	 * 
	 * @return
	 */
	public ThreadPoolExecutor getExecutor() {
		return poolExecutor;
	}

	// 创建一个缓冲池，缓冲池容量大小为Integer.MAX_VALUE
	// 遵循生产者-消费者县线程模式
	private static ThreadPoolExecutor newFixedThreadPool(int nThreads) {
		return new ThreadPoolExecutor(nThreads, nThreads, 0L,
				TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
	}

	// 创建固定容量大小的缓冲池
	// 不遵循生产者-消费者县线程模式
	private static ThreadPoolExecutor newCachedThreadPool() {
		return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L,
				TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
	}
}
